package goodenglish.interfaces;

import java.util.HashMap;
import java.util.Map;

/**
 * 只用于表现层，其它层用spring管理
 * 
 * @author hp
 * 
 */
public class BeanFactory {
	private static Map<Class, Object> singles = new HashMap<Class, Object>();

	/**
	 * 非线程安全
	 * 
	 * @param <T>
	 * @param requiredType
	 * @return
	 */
	public static <T> T getSingleInstance(Class<T> requiredType) {
		if (null == singles.get(requiredType)) {
			singles.put(requiredType, getBean(requiredType));
		}
		return (T) singles.get(requiredType);

	}

	private static <T> T getBean(Class<T> requiredType) {
		try {
			return requiredType.newInstance();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;

	}
}
